package com.yzw.phonestoreservice.controller;

import com.yzw.phonecommen.common.R;
import com.yzw.phonestoreservice.annotation.UnInterception;
import com.yzw.phonestoreservice.exception.PhoneException;
import com.yzw.phonestoreservice.form.OrderForm;
import com.yzw.phonestoreservice.pojo.OrderMaster;
import com.yzw.phonestoreservice.service.OrderMasterService;
import com.yzw.phonestoreservice.vo.OrderMasterVo;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @BelongsProject: phonestoresys
 * @BelongsPackage: com.yzw.phonestoreservice.controller
 * @Author: yanhongwei
 * @CreateTime: 2022-11-07  21:47
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping("phone/order")
@RestController
@Log4j2
public class OrderController {

    @Autowired
    private OrderMasterService orderMasterService;

    @PostMapping("/create")
    @UnInterception
    public R orderCreate(@Valid @RequestBody OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("创建订单数据校验错误");
            //bindingResult.getFieldError().getDefaultMessage()   得到校验直接上的 message 属性
            throw new PhoneException(bindingResult.getFieldError().getDefaultMessage());
        }
        String orderId = orderMasterService.orderCreate(orderForm);

        return R.ok().data("orderId",orderId);
    }
    // GET /order/detail
    @GetMapping("/detail/{orderId}")
    @UnInterception
    public R orderDetail(@PathVariable String orderId){
       OrderMasterVo orderMasterVo = orderMasterService.orderDetail(orderId);
        return R.ok().data("data",orderMasterVo);
    }
    @PutMapping("/pay/{orderId}")
    @UnInterception
    public R orderPay(@PathVariable String orderId){
        Boolean b = orderMasterService.orderPay(orderId);
        return b?R.ok():R.error();
    }

}
